<script setup lang="ts">
import { routes } from "../router/routes";
import { useRoute, useRouter } from "vue-router";
import { computed, reactive, ref } from "vue";
import { useStore } from "vuex";
import ACCESS_EUNM from "@/access/ACCESS_EUNM";
import verifyAccess from "@/access/VerifyAccess";

const router = useRouter();

const route = useRoute();
const selectKey = ref(["/"]);
//路由更新后，重新设置菜单高亮为当前页面选项。
router.afterEach((to, from, failure) => {
  selectKey.value = [to.path];
});
//点击跳转路由
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

//获取全局变量
const store = useStore();
//无法在已存在的对象上使用reactive：reactive只能用于初始化时创建新的响应式对象（经常出现的错误），
// 无法直接将已存在的对象转换为响应式对象,所以不能用reactive
const userName = computed(
  () => store.state.user?.loginUser?.userName ?? "未登录"
);
const loginUser = computed(() => store.state.user.loginUser);
//过滤不能展示以及权限不够的路由菜单
const realRoutes = computed(() => {
  return routes.filter((item) => {
    const needAccess = item.meta?.access ?? ACCESS_EUNM.LOGIN_FAILURE;

    if (item.meta?.needHide === true) {
      return false;
    }
    return verifyAccess(loginUser.value, needAccess as string);
  });
});

// setTimeout(() => {
//   store.dispatch("user/setLoginUser", {
//     name: "dastatham",
//     userRole: ACCESS_EUNM.ADMIN,
//   });
// }, 3000);
</script>

<template>
  <div class="all-container">
    <a-row id="globalHeader" align="center" :wrap="false">
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectKey"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title">
              <img src="../assets/NEUlogo.png" class="logo" />
              <div class="title-text">NEUOJ</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in realRoutes" :key="item.path" class="menu"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div>{{ userName }}</div>
      </a-col>
    </a-row>
  </div>
</template>

<style scoped>
.menu {
  font-size: 19px;
}

.all-container {
  overflow: hidden;
}

.title {
  height: 80px;
  display: flex;
  align-items: center;
}

.title-text {
  margin-left: 16px;
  color: #444;
  font-size: 35px;
  margin-top: 0;
}

#globalHeader .logo {
  margin-top: 0;
  height: 100px;
}
</style>
